package net.entelijan.cobean.notification;

import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JTextField;

import net.entelijan.cobean.core.ICobean;
import net.entelijan.cobean.core.impl.IPropertyChangeListenerAware;
import net.entelijan.cobean.text.DefaultTextInitializer;
import net.entelijan.cobean.util.CobeanUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TextNotificationTestFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	private static Log log = LogFactory.getLog(TextNotificationTestFrame.class);

	private JTextField leftTextField = createTextField("leftTextField");

	private JTextField rightTextField = createTextField("rightTextField");

	public TextNotificationTestFrame(String title) {
		super(title);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		final Container cp = getContentPane();
		cp.setLayout(new FlowLayout());
		cp.add(leftTextField);
		cp.add(rightTextField);
		pack();
	}

	private JTextField createTextField(String name) {
		final JTextField re = new JTextField();
		re.setColumns(20);
		re.setName(name);
		return re;
	}

	public static void main(String[] args) {
		TextNotificationTestFrame tf = new TextNotificationTestFrame(
				"Notification Test");
		tf.setVisible(true);
		final DefaultTextInitializer<String> ini = new DefaultTextInitializer<String>();
		ini.setCoTextComponent(tf.getLeftTextField());
		final ICobean<String> cobean = ini.initialize();
		log.debug("Created cobean: " + cobean);
		if (cobean instanceof IPropertyChangeListenerAware) {
			log.debug(cobean + " is IPropertyChangeListenerAware");
			((IPropertyChangeListenerAware) cobean)
					.setPropertyChangeListener(new RecordingPropertyChangeListener("irrelevant"));
		} else {
			log.debug(cobean + " is NOT IPropertyChangeListenerAware");
			CobeanUtil.addPropertyChangeListenerUsingReflection(cobean,
					new RecordingPropertyChangeListener("irrelevant"));
		}
		cobean.setValue("Hallo");
	}

	public JTextField getLeftTextField() {
		return leftTextField;
	}

	public void setLeftTextField(JTextField leftTextField) {
		this.leftTextField = leftTextField;
	}

	public JTextField getRightTextField() {
		return rightTextField;
	}

	public void setRightTextField(JTextField rightTextField) {
		this.rightTextField = rightTextField;
	}

}
